const appConfig = require('../../config/config.js');
const http = require('../../utils/http.js');
const nav = require('../../utils/navigator.js');
const util = require('../../utils/util.js');
// 引入SDK核心类
var QQMapWX = require('../../libs/qqmap-wx-jssdk.js');
//获取应用实例
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
    data: {
      to_flag:0,//跳转标记 1 从我的关注跳转过来
      noticeType:0,
      hiddenModal: true,
      pageIndex: {}, //当前页码
      pageData: {}, //分页数据
      loadMoreText: '正在载入更多...', //上拉加载提示文本
      hideBottom: true, //上拉加载提示层
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      console.log(options.userId, options.noticeType);
      
      var that = this;
      if (options.noticeType != null) {
          that.setData({ noticeType: options.noticeType });
      }
    if (options.to_flag != null) {
      that.setData({ to_flag: options.to_flag });
    }
    console.log(that.data.noticeType);
    wx.showLoading({
      title: '玩命加载中...',
    });
    var qqMap = new QQMapWX({
      key: appConfig.QQMapWXKey // 必填
    });
    app.getLocationInfo(function (data) {
      // 调用腾讯地图的接口查询当前位置
      qqMap.reverseGeocoder({
        location: {
          latitude: data.latitude,
          longitude: data.longitude
        },
        success: function (res) {
          console.log(res)
          //加载列表
          http.post(appConfig.baseUrl + 'Notice/QueryNoticeList', { 
            UserId: wx.getStorageSync('userId'),
            PageIndex: 1,
            NoticeType: that.data.noticeType
            },
            function (ret) { //
              console.log(ret.data);
              var result = that.calculateDistance(ret.data);
              wx.hideLoading(); //关闭加载动画
              that.setData({
                pageIndex: 1, //当前页码
                pageData: ret.data, //分页数据
                hideBottom: true,
                loadMoreText: '正在载入更多...'
              });

            });
        }
      });
    });
    
  },
  //列表点击事件
  item_click: function (e) {
    //console.log(e.currentTarget.dataset.noticeId);
    nav.navigateTo('../detail/detail', {
      noticeId: e.currentTarget.dataset.noticeId
    });
  },
  //距离计算
  calculateDistance: function (data) {
    var that = this;
    var result = data;
    var distance = 0;
    for (var i = 0; i < data.length; i++) {
      distance = util.getDistance(app.globalData.locationInfo.latitude, app.globalData.locationInfo.longitude, data[i].Latitude, data[i].Longitude);
      result[i].distance = distance;
    }
    return result;
  },
  bindChangeType:function(e)
  {
    var that = this;
    console.log(e.currentTarget.dataset.type);
    var type = e.currentTarget.dataset.type;
    that.setData({ noticeType: type, pageIndex: 1, pageData: [] });
    this.onSearch();
    // if (type == 2) {
    //   nav.navigateTo("../myfavorite/myfavorite?noticeType=" + type);
    // } else {
    //   nav.navigateTo("../mynotice/mynotice?noticeType=" + type+"&to_flag="+this.data.to_flag);
    // }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    //wx.reLaunch({ url: '../index/index?noticeType=0' });
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  onSearch: function (e) {
    var that = this;
    console.log(that.data.noticeType)
    wx.showLoading({
      title: '玩命加载中...',
    });
    http.post(appConfig.baseUrl + 'Notice/QueryNoticeList', {
      UserId: wx.getStorageSync('userId'),
      PageIndex: 1,
      NoticeType: that.data.noticeType
    },
      function (res) { //
        console.log(res.data);
        wx.hideLoading(); //关闭加载动画
        var result = that.calculateDistance(res.data);
        //console.log(result);
        that.setData({
          pageIndex: 1, //当前页码
          pageData: res.data, //分页数据
          hideBottom: true,
          loadMoreText: '正在载入更多...'
        });

      },
      function (res) {
        console.log(res);
        wx.hideLoading(); //关闭加载动画
      });
  },
  // 下拉刷新
  onPullDownRefresh: function (e) {
    var that = this;
    // 显示顶部刷新图标
    wx.showNavigationBarLoading();

    http.post(appConfig.baseUrl + 'Notice/QueryNoticeList', {
      UserId: wx.getStorageSync('userId'),
      PageIndex: 1,
      NoticeType: that.data.noticeType
    },
      function (res) { //
        console.log(res.data);
        var result = that.calculateDistance(res.data);
        that.setData({
          pageIndex: 1, //当前页码
          pageData: res.data, //分页数据
          hideBottom: true,
          loadMoreText: '正在载入更多...'
        });
        // 隐藏导航栏加载框
        wx.hideNavigationBarLoading();
        // 停止下拉动作
        wx.stopPullDownRefresh();
      });

  },
  //上拉加载
  onReachBottom: function (e) {
    var that = this;
    // 页数+1
    that.setData({
      pageIndex: that.data.pageIndex + 1,
      hideBottom: false
    });
    wx.showLoading({
      title: '玩命加载中...',
    });
    //console.log(that.data.pageIndex);
    http.post(appConfig.baseUrl + 'Notice/QueryNoticeList', {
      UserId: wx.getStorageSync('userId'),
      PageIndex: that.data.pageIndex,
      NoticeType: that.data.noticeType
    },
      function (res) { //
        wx.hideLoading(); //关闭加载动画
        //console.log(res.data);
        var result = that.calculateDistance(res.data);
        if (res.data.length > 0) {
          var data_list = that.data.pageData;
          for (var i = 0; i < res.data.length; i++) {
            data_list.push(res.data[i]);
          }
          that.setData({
            pageData: data_list, //分页数据
            hideBottom: true
          });
          //console.log(that.data.pageData);
        } else {
          //没有更多了
          that.setData({
            loadMoreText: "没有更多数据了", //分页数据
            hideBottom: false,
            pageIndex: that.data.pageIndex - 1
          });
        }
      });
  }
})